package com.coderyeah.cosmetology.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.coderyeah.cosmetology.entity.Project;
import com.coderyeah.cosmetology.mapper.ProjectMapper;
import com.coderyeah.cosmetology.service.ProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coderyeah.cosmetology.util.Result;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lqs
 * @since 2022-05-24
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result getProInfo() {
        String key = "CACHE:PROS";
        String s = stringRedisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(s)) {
            List<Project> projects = JSONUtil.toList(s, Project.class);
            return Result.success(projects);
        }
        List<Project> projects = projectMapper.getProInfo();
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(projects), 30, TimeUnit.MINUTES);
        return Result.success(projects);
    }

    @Override
    public Result getProInfoById(String id) {
        Project project = projectMapper.getProInfoById(id);
        return Result.success(project);
    }

}
